import Utils from "../utils";
import AnimationAction from "./AnimationAction";
import GameConst from "../GameConst";

export default class AnimatedNode extends Laya.Sprite {
    constructor(t, n, r) {
        super();
        this.animation = t;
        this.parts = new Array;
        this.isPlaying = true;
        this.actions = new Array;
        this.hasCycle = false;
        this.owner = null;
        this.skins = new Array;
        this.frameSelector = r;
        this.initFrameDelay = n;
        this.initParts();
        this.mouseChildren = false;
    }
}
AnimatedNode.prototype.createUsualSprite = function (name) {
    return Utils.createSpriteFromSpritesheet(name);
};
AnimatedNode.prototype.initParts = function () {
    this.setFrameDelay(this.animation.forceFrameDelay > 0 ? this.animation.forceFrameDelay : Math.abs(this.initFrameDelay) < 1e-10 ? 1 / GameConst.DESIGN_FPS : this.initFrameDelay);
    this.totalFrames = 1;
    for (var e = 0; e < this.animation.layers.length; e++) {
        var t = this.animation.layers[e];
        if (this.frameSelector && !this.frameSelector.testLayer(this.animation.name))
            continue;
        this.totalFrames = Math.max(this.totalFrames, t.frames.length);
        var n = this.frameSelector ? this.frameSelector.getFrame(this, t) : 0;
        if (n != -1) {
            this.skins.push(n);
            var r = new Laya.Sprite();
            var i = null;
            var s = t.getClipData(n);
            i = this.createUsualSprite(s.name);
            i.pivotX = i.width * s.anchor.x;
            i.pivotY = i.height * (1 - s.anchor.y);
            r.addChild(i);
            this.parts.push(r);
            this.addChild(r);
        } else {
            this.skins.push(-1);
            this.parts.push(null);
        }
    }
    this.gotoAndPlay(0);
    if (this.animation.transform)
        this.animation.transform.applyTransform(this);
    if (this.totalFrames <= 1)
        this.stop();
    if (this.frameSelector) {
        this.frameSelector = null;
    }
    if (this.animation && this.animation.isAdd) {}

    if (this.animation && this.animation.isOverlay) {}

};
AnimatedNode.prototype.getFrameByPartIndex = function (e) {
    var t = this.animation;
    var n = e;
    var r = t.layers[n].frames[this.currentFrame];
    return r;
};
AnimatedNode.prototype.disableLayer = function (e) {
    var t = this.getPartByFlashName(e);
    if (t){
        Utils.removeClip(t);
    }
};

AnimatedNode.prototype.update = function (deta) {

    Utils.limitDt(deta);
    if (this.isPlaying) {
        this.currentDelay -= deta;
        while (this.currentDelay <= 0) {
            this.gotoAndPlay(this.currentFrame + 1);
            for (var t = 0; t < this.actions.length; t++)
                this.actions[t].checkAction();
            this.currentDelay += this.frameDelay;
            if (this.currentFrame == 0)
                this.hasCycle = true;
        }
    }
};
AnimatedNode.prototype.setRandomFrame = function () {
    var e = Utils.getInt(this.totalFrames);
    if (this.isPlaying)
        this.gotoAndPlay(e);
    else
        this.gotoAndStop(e);
};
AnimatedNode.prototype.setFrameDelay = function (e) {
    this.currentDelay = this.frameDelay = e;
};
AnimatedNode.prototype.setFps = function (e) {
    this.setFrameDelay(1 / e);
};
AnimatedNode.prototype.isOnLastFrame = function () {
    return this.currentFrame == this.totalFrames - 1;
};
AnimatedNode.prototype.setOwner = function (e) {
    this.owner = e;
};
AnimatedNode.prototype.getOwner = function () {
    return this.owner;
};
AnimatedNode.prototype.gotoAndPlay = function (e) {
    this.play();
    this.currentFrame = Math.max(0, e);
    this.currentFrame = this.currentFrame % this.totalFrames;
    for (var t = 0; t < this.parts.length; t++) {
        var n = this.parts[t];
        if (n) {
            var r = this.animation;
            var i = t;
            var s = r.layers[i].frames[this.currentFrame];
            s.applyTransform(n);
            if (this.owner)
                n.visible = n.visible && this.owner.canBeVisible(n);
        }
    }
    var o = this.currentFrame;
    for (t = 0; t < this.actions.length; t++) {
        this.actions[t].checkAction();
        if (this.currentFrame != o)
            return;
    }
};
AnimatedNode.prototype.gotoAndStop = function (e) {
    this.gotoAndPlay(e);
    this.stop();
};
AnimatedNode.prototype.play = function () {
    if (!this.isPlaying)
        this.resetFrameDelay();
    this.isPlaying = true;
};
AnimatedNode.prototype.stop = function () {
    this.isPlaying = false;
    this.resetFrameDelay();
};
AnimatedNode.prototype.resetFrameDelay = function () {
    this.currentDelay = this.frameDelay;
};
AnimatedNode.prototype.getCurrentDelay = function () {
    return this.currentDelay;
};
AnimatedNode.prototype.setCurrentDelay = function (e) {
    this.currentDelay = this.frameDelay * e;
};
AnimatedNode.prototype.getFloatFrame = function () {
    return this.currentFrame + limit((this.frameDelay - this.currentDelay) / this.frameDelay, 0, 1);
};
AnimatedNode.prototype.setPartSkin = function (e, t, n) {
    if (typeof n === "undefined") {
        n = false;
    }
    var r = this.animation.getLayerByFlashName(e);
    var i = this.parts[r].getChildAt(0);
    var s = this.animation.layers[r];
    if (n)
        t = s.clipDatas.length + t;
    this.skins[r] = t;
    var o = s.getClipData(t);
    i.gotoAndStop(o.name);
    // i.pivotX = i.getBounds().width * o.anchor.x;
    // i.pivotY = i.getBounds().height * (1 - o.anchor.y);
};
AnimatedNode.prototype.getPart = function (e) {
    return this.parts[e];
};
AnimatedNode.prototype.getPartByFlashName = function (e) {
    return this.getPart(this.animation.getLayerByFlashName(e));
};
AnimatedNode.prototype.getPartSkin = function (e) {
    return this.skins[e];
};
AnimatedNode.prototype.getPartSkinByName = function (e) {
    return this.getPartSkin(this.animation.getLayerByFlashName(e));
};
AnimatedNode.prototype.getSkinByFlashName = function (e) {
    return this.getPartSkin(this.animation.getLayerByFlashName(e));
};
AnimatedNode.prototype.updateOwnerVisibility = function () {
    for (var e = 0; e < this.parts.length; e++) {
        var t = this.parts[e];
        if (t && this.owner)
            t.visible = t.visible && this.owner.canBeVisible(t);
    }
};
AnimatedNode.prototype.destroy = function () {
    this.skins = null;
    this.frameSelector = null;
    for (var e = 0; e < this.parts.length; e++) {
        if (this.parts[e])
        Utils.removeClip(this.parts[e]);
    }
    this.owner = null;
    this.parts = null;
    this.actions = null;
};
AnimatedNode.prototype.addAction = function (e, t, n, r) {
    if (typeof n === "undefined") {
        n = -1;
    }
    if (typeof r === "undefined") {
        r = 1;
    }
    var i = new AnimationAction(e, t, this, n, r);
    this.actions.push(i);
    return i;
};